package steed.test.redis;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.junit.Test;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import steed.ext.domain.user.Role;
import steed.ext.domain.user.User;
import steed.hibernatemaster.sample.domain.School;
import steed.hibernatemaster.sample.domain.Student;
import steed.test.SteedTest;
import steed.util.base.test.TestEfficiency;
import steed.util.redis.RedissonUtil;

public class Redission extends SteedTest{
	
	@Test
	public void baseTest(){
		RedissonClient redisson = RedissonUtil.getRedisson();
		RBucket<Object> bucket = redisson.getBucket("foo");
		bucket.set("bar");
		assert("bar".equals(redisson.getBucket("foo").get()));
		redisson.getMap("redissonTestMap").put("school", new School());
		ArrayList<Object> arrayList = new ArrayList<>();
		arrayList.add(new Student());
		redisson.getMap("redissonTestMap").put("list", arrayList);
		
		assert(redisson.getMap("redissonTestMap").get("school") != null);
		assert(RedissonUtil.getRedisson().getMap("redissonTestMap").get("school") != null);
		assert(RedissonUtil.getRedisson().getMap("redissonTestMap").get("list") != null);
		assert(((ArrayList)RedissonUtil.getRedisson().getMap("redissonTestMap").get("list")).get(0) != null);
	}
	
	@Test
	public void threadTest(){
		RedissonUtil.getRedisson().getMap("threadTest");
	}
	
	@Test
	public void testRAM() throws FileNotFoundException{
		TestEfficiency testEfficiency = new TestEfficiency();
		testEfficiency.begin();
		save();
		testEfficiency.endAndOutUsedTime("用时");
	}

	private void save() {
		Map<Object, Object> map = RedissonUtil.getRedisson().getMap("testRAM");
		for (int i = 0; i < 1000000; i++) {
			User user = new User();
			user.setName("user"+i);
			user.setPassword("ddddddddddddsaudsofsdsafdsa"+i);
			user.setPhoneNumber("1jd2827393000");
			user.setIntegration(i+1L);
			user.setStatus(i);
			user.setUserType(i);
			user.setE_mail("battle_steed@163.com");
			HashSet<Role> roleSet = new HashSet<>();
			roleSet.add(getRole(i));
			roleSet.add(getRole(i));
			
			user.setRoleSet(roleSet);
			
			user.setNickName("nickName"+i);
//			map.put("user"+i, user);
			assert(((User)map.get("user"+i)).getName().equals("user"+i));
		}
	}

	private Role getRole(int i) {
		Role role = new Role();
		role.setName("管理员"+i);
		role.setIsSystemRole(true);
		return role;
	}
	
}
